﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Resources;
using System.Text;
using AiFrame.InterfaceLib.Plugins;
using AiFrame.InterfaceLib.Resources;
using AiFrame.InterfaceLib.Windows.Controls;
using AiFrameDemo.CustomerData.Core.DataStore;
using AiFrameDemo.CustomerData.Core.ViewModel;
using AiFrameDemo.CustomerData.Core.Views;
using AiFrameDemo.Lib.Model;

namespace AiFrameDemo.CustomerData.Core
{
    public class CustomerDataController
    {
        private ICoreInterface _ci;

        public void Load(ICoreInterface ci, string programPath)
        {
            // Save the CoreInterface
            _ci = ci;

            // Initialize DaoFactory.
            DaoFactory.Initialize(ci.DatabaseConnection);

            // Initialize the registry in order to save the CoreInterface and program path
            // there.
            CustomerDataRegistry.Instance.Initialize(ci, programPath);

            // Register the customer class.
            ci.DatabaseConnection.AddMappingClass(typeof(Customer));

            ResourceImages resourceImages = new ResourceImages();

            ResourceManager resourceManager = new ResourceManager("AiFrameDemo.CustomerData.Core.Properties.Resources", this.GetType().Assembly);

            // Create a new data navigation bar.
            IDataNavigationBar dataNavigationBar = ci.NavigationBar.CreateDataNavigationBar();
            dataNavigationBar.AddButton("printCustomerButton", "Print datasets", resourceImages.ListPrint, null, delegate{});
            dataNavigationBar.AddButton("newCustomerButton", "Add new customer", resourceImages.DatasetAdd, null, delegate {});
            ci.NavigationBar.AddButton("customerDataButton", "Customers", null, (Image) resourceManager.GetObject("customers"), dataNavigationBar, OnNavBarButtonClick);
            
            // Register menu items.
            object menuItem = ci.MainMenu.AddMainMenuItem("Customers", "customerDataMenuItem", "Customers",
                                                          (object) null, null);
            ci.MainMenu.AddMainMenuItem("Add new customer...", "newCustomerMenuItem", "Add new customer", menuItem,
                                        OnNewCustomerMenuItemClick);

            
        }

        private void OnNewCustomerMenuItemClick(object sender, EventArgs e)
        {
            OnNavBarButtonClick(null, new EventArgs());
        }

        private void OnNavBarButtonClick(object sender, EventArgs e)
        {
            // Check if a customer window exists already    
            if (!_ci.WindowManager.ExistsWindow<ICustomerDataView>())
            {
                ICustomerDataView customerView = CustomerFactories.CustomerDataViewFactory.CreateCustomerView();
                CustomerDataViewModel viewModel = new CustomerDataViewModel(customerView);

                _ci.WindowManager.RegisterWindow(customerView);
            }else
                _ci.WindowManager.BringWindowToFront<ICustomerDataView>();
        }
        
        public void Unload()
        {
            // TODO: Unregister
            // Do something
        }
    }
}
